<?php
	// Note: Loads TwitterAPI from upper directory (SVN structure)
	require('../TwitterAPI.php');
	ob_start();
	
	//
	// Used to output messages to the browser in real-time
	//
	function sendOutput($function, $message)
	{
		echo '<div class="output">', $function, '<p class="function_output">', nl2br(highlight_string('<?php'."\n".var_export($message, true)."\n".'?>', true)), '</p></div>';
		ob_flush();
		flush();
		
		// Wait a second for each tested function
		sleep(1);
	}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<title>PHP Twitter API - Test Run</title>
	<style type="text/css" media="screen">
		body {
			font-size: 14px;
			font-family: Tahoma, Arial;
		}
		
		#content {
			width: 800px;
			margin: 0 auto;
		}
		
		#header {
			font-size: 22px;
			font-family: Courier;
			color: #C41A1A;
			font-weight: bold;
			text-decoration: underline;
		}
		
		#testing_area { 
			padding-left: 20px;
			padding-right: 20px;
			padding-top: 10px;
			padding-bottom: 10px;
			border: 1px solid #B8B88E;
			border-left: 3px solid #B8B88E;
			width: 800px;
			line-height: 1.5em;
		}
		
		#login_area {
			width: 50%;
			margin: 0 auto;
			border: 1px solid #6FC344;
			border-left: 3px solid #6FC344;
			background-color: #D4FFD4;
			padding-left: 10px;
			padding-right: 10px;
		}
		
		.output {
			font-size: 18px;
			font-weight: bold;
			font-family: Courier;
			margin-bottom: 15px;
		}
		
		.function_output {
			font-size: 10px;
			font-weight: normal;
			font-family: Tahoma;
			padding-left: 10px;
			width: 600px;
			width: 100%;
			border: 1px solid #D9D9D9;
			border-left: 3px solid #D9D9D9;
			background-color: #EEEEEE;
		}
	</style>
</head>

<body>
	<div id="content">
		<p id="header">
			PHP Twitter API :: Test Run
		</p>
		<p>
			This exorcise tests all of the implemented API functions within the PHPTwitterAPI class.<br /><br />
			<strong>Below</strong> is a test-bed. In order to run the tests there needs to be a set of valid Twitter credentials.<br />
			Simply input a valid Twitter username and password into their respective boxes below and click "Begin Tests". Please note<br />
			that these tests use up API calls. Each Twitter account is allowed 1,000 API calls per hour, please keep this in mind.<br />
			<br />
			<span style="text-decoration: underline">Estimated API Calls:</span> 12
			<br />
			<br />
		</p>
		
		<div id="testing_area">
			<div id="login_area">
				<form method="post" action="">
					<p>
<?php
	if(isset($_POST['submit']))
	{
		$twitter = new TwitterAPI($_POST['username'], $_POST['password']);
		
		if(!$twitter->verifyCredentials())
		{
			echo '<span style="color: #C81E1E; padding-left: 45px;">[Error] Incorrect login credentials, please try again.</span><br/>';
		}
		else
		{
			define('AUTHED', 1);
		}
	}
?>
						<label for="_frm1"><strong>Username |</strong></label> <input type="text" id="_frm1" name="username" maxlength="32" value="<?php isset($_POST['username']) ? print($_POST['username']) : '' ?>" /><br />
						<label for="_frm2"><strong>Password  |</strong></label> <input type="password" id="_frm2" name="password" maxlength="32" value="<?php isset($_POST['password']) ? print($_POST['password']) : '' ?>" />
						<span style="position: absolute; width: 100%; margin-left: 38px;margin-top: -20px;">
							<input style="height: 40px;" type="submit" id="_frm3" name="submit" value="Begin Tests" />
						</span>
					</p>
				</form>
			</div>
			<br />

<?php
	if(defined('AUTHED'))
	{
		// Along the way we will want to store some vars
		$vars = array();
		
		try
		{
			//
			// Tweet functions
			//
			sendOutput('sendTweet(string $message)', $twitter->sendTweet('(think nothing of this)') ? 'true' : 'false');
			sendOutput('getFriendTweets([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $twitter->getFriendTweets());
			sendOutput('getPublicTweets([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $twitter->getFriendTweets());
			$vars['buffer'] = $twitter->getTweets();
			$vars['tweetid'] = $vars['buffer'][0]['id'];
			sendOutput('getTweets([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $vars['buffer']);
			sendOutput('getTweet(int $tweetid[, string $date_format = null])', $twitter->getTweet($vars['tweetid']));
			sendOutput('deleteTweet(int $tweet_id)', $twitter->deleteTweet($vars['tweetid']));
			
			//
			// Friend functions
			//
			sendOutput('addFriend($user[, string $user_identifier = \'screen_name\'[, string $follow_user = \'true\']])', $twitter->addFriend('JStrese', 'screen_name', 'false'));
			sendOutput('getFriendIDList([array $params = null])', $twitter->getFriendIDList());
			sendOutput('existsFriend(string $friend)', $twitter->existsFriend('JStrese'));
			sendOutput('deleteFriend(string $friend[, string $friend_identifier = \'screen_name\'])', $twitter->deleteFriend('JStrese'));
			
			//
			// Follower functions
			//
			sendOutput('getFollowers([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $twitter->getFollowers());
			sendOutput('getFollowerIDList([array $params = null])', $twitter->getFollowerIDList());
			
			//
			// DirectMessage functions
			//
			sendOutput('sendDirectMessage(string $recipient, string $message)', $twitter->sendDirectMessage('JStrese', 'This is what you get for putting yourself in a test suite!'));
			// sendOutput('deleteDirectMessage(int $message_id)', $twitter->deleteDirectMessage(-1)); Lets just assume that this works :-)
			sendOutput('getDirectMessages([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $twitter->getDirectMessages());
			sendOutput('getSentDirectMessages([string $date_format = null[, array $params = null[, bool $use_href = true]]])', $twitter->getSentDirectMessages());
			
			//
			// Miscellaneous functions
			//
			sendOutput('getUser(string $user[, string $user_identifier = \'screen_name\'[, string $date_format = null]])', $twitter->getUser('JStrese'));
			sendOutput('getMentioned([string $date_format = null[, array $params = null[, bool $truncate_me = true[, bool $use_href = true]]]])', $twitter->getMentioned());
			sendOutput('getRateLimit()', $twitter->getRateLimit());
			sendOutput('helpTest()', $twitter->helpTest());
		}
		catch(Exception $exception)
		{
			echo 'The test suite was interrupted by an error: <br/>', $exception->getMessage();
		}
	}
	else
	{
?>
			<div class="output">
				example_function()
				<p class="function_output">
					A functions output will be displayed in here.
				</p>
			</div>
<?php
	}
?>			
		</div>
	</div>
</body>
</html>